package com.pagoda.nerp.trade.common.wx.thread;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pagoda.nerp.trade.common.httputil.HttpClientHelper;
import com.pagoda.nerp.trade.common.objectutil.PropertyConfigurer;
import com.pagoda.nerp.trade.common.wx.module.InterfaceDefine;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 业务与微信服务器通信功能
 *
 * Created by Jungle on 2016/12/21.
 */
public class WxMessagePushJob implements Runnable {

    private static final Logger logger = LoggerFactory.getLogger(WxMessagePushJob.class);

    private String wxOrgId ;
    private String wxAppId ;
    private String wxAppSecret;
    private String platType;
    private String templateId;
    private String wxUrlPrefix;

    /**
     * 发送模板消息的请求 data 数据块 { "remark' : {}}
     */
    private Map dataMap ;
    private String url;
    private String toUser;

    public WxMessagePushJob() {
    }

    public WxMessagePushJob(Map dataMap, String clickUrl, String toUser) {
        this.templateId = PropertyConfigurer.parseStr("wx.templateID");
        this.initConfigure(dataMap,toUser,clickUrl);
    }

    public WxMessagePushJob(Map dataMap,String clickUrl,String toUser,String templateId) {
        this.templateId = templateId;
        this.initConfigure(dataMap,toUser,clickUrl);
    }

    private void initConfigure(Map dataMap, String toUser, String clickUrl){
        this.dataMap = dataMap;
        this.url = clickUrl;
        this.toUser = toUser;
        this.platType = PropertyConfigurer.parseStr("wx.platType");
        this.wxAppId = PropertyConfigurer.parseStr("wx.appID");
        this.wxAppSecret = PropertyConfigurer.parseStr("wx.appsecret");
        this.wxOrgId = PropertyConfigurer.parseStr("wx.orgID");
        this.wxUrlPrefix = PropertyConfigurer.parseStr("wx.wxgateway.url");
    }

    @Override
    public void run() {
        // 配置校验
        boolean checkRes = this.checkConfig();
        if(!checkRes){
            logger.error("WxMessagePushJob => run(); The configure if not complete . Send Fail ");
            return;
        }
       try {
            // 发送消息
            String reqBody = this.buildTmpMsgParam();
            logger.info("发送模板消息，请求报文:{}",reqBody);
            String responseStr = HttpClientHelper.invoke(this.wxUrlPrefix + InterfaceDefine.CON_TEMPLATE_REQUEST_URL, reqBody);
            logger.info("WxMessagePushJob send complete ! response info : {}",responseStr);
        }catch(Exception e){
            logger.error("WxMessagePushJob send not success !",e);
        }
    }

    private String buildTmpMsgParam(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("platType", this.platType);
        jsonObject.put("wxOrgID", this.wxOrgId);
        jsonObject.put("toUser", this.toUser);
        jsonObject.put("template_id", this.templateId);
        jsonObject.put("url", StringUtils.isNotEmpty(this.url) ? this.url : "");
        JSONObject dataJson = (JSONObject) JSON.toJSON(this.dataMap);
        jsonObject.put("data", this.dataMap);
        return jsonObject.toString();
    }

    private boolean checkConfig(){
        if(!StringUtils.isNotEmpty(this.templateId)){
            logger.error("WxMessagePushJob => wx.templateId is not configure. Send Fail ");
            return false;
        }
        if(!StringUtils.isNotEmpty(this.platType)){
            logger.error("WxMessagePushJob => wx.templateId is not configure. Send Fail ");
            return false;
        }
        if(!StringUtils.isNotEmpty(this.wxAppId)){
            logger.error("WxMessagePushJob => wx.appId is not configure. Send Fail ");
            return false;
        }
        if(!StringUtils.isNotEmpty(this.wxAppSecret)){
            logger.error("WxMessagePushJob => wx.appSecret is not configure. Send Fail ");
            return false;
        }
        if(!StringUtils.isNotEmpty(this.wxOrgId)){
            logger.error("WxMessagePushJob => wx.orgId is not configure. Send Fail ");
            return false;
        }
        return true;
    }
}
